﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microserve.TeleEye.BusinessServices;
using System.Drawing;
using System.Xml;
using Microserve.TeleEye.Common.Utility;

namespace Microserve.TeleEye.Web
{
    public partial class ContactUs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sectionname = "wheretobuyhome";

            if (Utility.Validators.ValidateSectionName(sectionname))
            {
                MenuController controller = new MenuController();
                List<Microserve.TeleEye.Common.MenuBar.MenuItem> items = controller.GetSectionMenu(HttpContext.Current,
                    sectionname);

                this.toolboxlist.Controls.Add(new LiteralControl()
                {
                    Text =
                        Utility.MenuFactory.GenerateMenu(items)
                });


                FeaturedProductManager fpManager = new FeaturedProductManager();
                List<Microserve.TeleEye.Common.Products.Product> fProducts = fpManager.GetFeaturedProducts(HttpContext.Current,
                    sectionname);

                this.featuredProducts.DataSource = fProducts;
                this.featuredProducts.DataBind();

                SectionManager sManager = new SectionManager();
                this.sectionTitle.InnerHtml = sManager.GetSectionTitle(HttpContext.Current, sectionname);
                this.rightTitle.InnerHtml = sManager.GetSectionSubtitle(HttpContext.Current, sectionname);
                this.sectionAbstract.InnerHtml = sManager.GetSectionAbstract(HttpContext.Current, sectionname);

            }
            else
            {
                //do nothing...
            }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            //ccJoin.ValidateCaptcha(txtCaptcha.Text);
            if (Page.IsValid)
            {
                string toAddress = this.txtEmail.Text;
                string fromAddress = "contact@teleeye.com.ph";

                XmlDocument config = new XmlDocument();
                config.Load(PathHelper.GetSectionPath(HttpContext.Current, "emailsettings"));

                XmlNode from = config.SelectSingleNode("settings/client/from");
                if (from != null)
                {
                    fromAddress = from.InnerText;
                }

                System.Net.NetworkCredential c = new System.Net.NetworkCredential();
                c.Password = "3mm13lee";
                c.UserName = "contact@teleeye.com.ph";
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = c;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.EnableSsl = true;
                smtp.Port = 587;
                smtp.Host = "smtp.gmail.com";
                //System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                //smtp.EnableSsl = true;
                //smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                //smtp.UseDefaultCredentials = true;

                System.Net.Mail.MailMessage mm2 = new System.Net.Mail.MailMessage();
                mm2.From = new System.Net.Mail.MailAddress(fromAddress);

                XmlNodeList addresses = config.SelectNodes("settings/contactus/addresses/address");
                foreach (XmlNode address in addresses)
                {                    
                    mm2.To.Add(address.InnerText);                   
                }

                XmlNode copySubject = config.SelectSingleNode("settings/contactus/subject");
                if (copySubject != null)
                {
                    mm2.Subject = string.Format(copySubject.InnerText, this.txtCompanyName.Text, this.txtName.Text);
                }

                XmlNode copyBody = config.SelectSingleNode("settings/contactus/body");
                if (copyBody != null)
                {
                    mm2.Body = copyBody.InnerXml.Replace("{0}",
                        this.txtName.Text).Replace("{1}",
                        this.txtCompanyName.Text).Replace("{2}",
                        this.txtContactNumber.Text).Replace("{3}",
                        this.txtEmail.Text).Replace("{4}",
                        this.txtIndustry.Text);
                }
                mm2.IsBodyHtml = true;

                try
                {
                    smtp.Send(mm2);

                    this.lblErrorMessage.Text = "Your message has been sent. Thank you for your interest in our product.";
                }
                catch
                {
                }
            }
            else
            {
                this.txtCaptcha.Text = string.Empty;                   
                this.lblErrorMessage.ForeColor = Color.Red;
                this.lblErrorMessage.Visible = true;
                this.lblErrorMessage.Text = "CAPTCHA Validation failed. Please reenter the word. Sorry for the inconvinience.";
            }
            this.txtCaptcha.Text = string.Empty;

        }
    }
}